package edu.sysu.cshare.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;

import edu.sysu.cshare.control.UserManager;

public class Dlg_ChangePsw extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JPasswordField pNew2;
	private JPasswordField pNew1;
	private JPasswordField pOld;
	private UserManager userManager;
	/**
	 * Launch the application.
	 */


	/**
	 * Create the dialog.
	 */
	public Dlg_ChangePsw(UserManager userManager) {
		setForeground(new Color(30, 144, 255));
		setBackground(new Color(218, 112, 214));
		this.userManager=userManager;
		
		setBounds(100, 100, 277, 201);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(new Color(135, 206, 250));
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		setLocationRelativeTo(null);
		contentPanel.setLayout(null);
		{
			JLabel lblNewLabel = new JLabel("旧密码:");
			lblNewLabel.setBackground(new Color(0, 0, 255));
			lblNewLabel.setBounds(10, 14, 76, 14);
			contentPanel.add(lblNewLabel);
		}
		{
			JLabel label = new JLabel("新密码:");
			label.setBackground(new Color(135, 206, 235));
			label.setBounds(10, 59, 76, 14);
			contentPanel.add(label);
		}
		{
			JLabel label = new JLabel("确认输入:");
			label.setBounds(10, 102, 76, 14);
			contentPanel.add(label);
		}
		
		pOld = new JPasswordField();
		pOld.setBackground(new Color(245, 255, 250));
		
		pOld.setBounds(96, 14, 142, 20);
		contentPanel.add(pOld);
		
		pNew1 = new JPasswordField();
		
		
		pNew1.setBounds(96, 56, 142, 20);
		contentPanel.add(pNew1);
		
		pNew2 = new JPasswordField();
		pNew2.setBounds(96, 99, 142, 20);
		contentPanel.add(pNew2);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBackground(new Color(135, 206, 250));
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("确定");
				okButton.setBackground(new Color(0, 0, 255));
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						if((String.valueOf(pOld.getPassword()).equals(""))){
							Dlg_ChangePsw.this.dispose();
						}
						
						if(!Dlg_ChangePsw.this.userManager.pwdValid(String.valueOf(pOld.getPassword()))){
							JOptionPane.showMessageDialog(null, "旧密码输入错误");
							pOld.setText("");
							return;
						}
						
						String p1 = String.valueOf(pNew1.getPassword());
						String p2 = String.valueOf(pNew2.getPassword());
						if(p1.equals(p2)){
							Dlg_ChangePsw.this.userManager.changePassword(p1);
							JOptionPane.showMessageDialog(null, "修改成功");
							Dlg_ChangePsw.this.dispose();
						}else{
							JOptionPane.showMessageDialog(null, "两次输入不一致");
						}
					}
				});
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("取消");
				cancelButton.setBackground(new Color(0, 0, 255));
				cancelButton.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						Dlg_ChangePsw.this.dispose();
					}
				});
				buttonPane.add(cancelButton);
			}
		}
	}
}
